package com.example.service.serviceImp;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.dao.NewPhotoMapper;
import com.example.pojo.NewPhoto;
import com.example.service.NewPhotoService;
import com.example.util.PhotoUtil;
import com.example.util.Qiniu;
import com.example.util.Transform;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Date;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service("PhotoService")
public class NewPhotoServiceImpl implements NewPhotoService {

    private NewPhotoMapper newPhotoMapper;
    @Override
    public Object findAllPhoto(JSONObject json) {
        String userId=json.getString("userId");
        QueryWrapper<NewPhoto> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("userId",userId);
        return newPhotoMapper.selectList(queryWrapper);
    }

    @Override
    public Object addNewPhoto(String url,String type){
        LocalDate localDate = LocalDate.now();
        Date date = Date.valueOf(localDate);
        NewPhoto newPhoto=new NewPhoto();
        newPhoto.setUptime(date);
        newPhoto.setUrl(url);
        newPhoto.setPhotoType(type);
        newPhotoMapper.insert(newPhoto);
        return newPhoto;
    }

    @Override
    public String uploadPath(String newPhotoPath){
        //String imgStr= json.getString("imgStr");
        //NewPhoto newPhoto=JSONObject.parseObject(json.toJSONString(),NewPhoto.class);
        String url= (String) Qiniu.upload(newPhotoPath);
        return url;
    }

    @Override
    public Object transform(MultipartFile upload) throws IOException {

        String blackPath="src/main/resources/static/blankPhoto/";//转化黑色图片目录
        String photoUrl=PhotoUtil.download(upload);
        File img=new File(photoUrl);
        PhotoUtil.reduceImg(photoUrl,photoUrl,100,200,0.2F);//缩写图片
        String black =PhotoUtil.changeColor(img,blackPath);//将图片转化成黑色
        String url=Transform.enter(black);//转化成骰子图
        return url;
    }

    @Override
    public void uploadPhoto(Map<String, Object> photo) {

    }

    @Override
    public void deletePhoto(Map<String, Object> photo) {

    }
}
